package cn.sicnu.cs.employment.service.impl;

import cn.sicnu.cs.employment.domain.entity.BgAppeal;
import cn.sicnu.cs.employment.domain.entity.EmployeeExpProject;
import cn.sicnu.cs.employment.exception.CustomException;
import cn.sicnu.cs.employment.mapper.AdminRoleMapper;
import cn.sicnu.cs.employment.mapper.BgAppealMapper;
import cn.sicnu.cs.employment.mapper.EmployeeExpMapper;
import cn.sicnu.cs.employment.service.IBgAppealService;
import cn.sicnu.cs.employment.service.ICompanyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static cn.sicnu.cs.employment.common.Constants.ERROR_CODE;

@Slf4j
@Service
@RequiredArgsConstructor
public class BgAppealServiceImpl implements IBgAppealService {

    private final ICompanyService companyService;
    private final BgAppealMapper appealMapper;
    private final AdminRoleMapper adminRoleMapper;
    private final EmployeeExpMapper expMapper;


    @Override
    public List<BgAppeal> getAppealsByBgId(Long bgId) {
        QueryWrapper<BgAppeal> wrapper = new QueryWrapper<>();
        wrapper.eq("bg_id", bgId)
                .eq("status", 1);
        return appealMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void saveAppeal(BgAppeal appeal, EmployeeExpProject project) {
        if (appeal.getId() != null) {
            appealMapper.updateById(appeal);
            return;
        }
        appealMapper.insert(appeal);
        if (appeal.getType() == 1 && project != null){
            expMapper.insert(project);
        }
    }

    @Override
    public List<BgAppeal> getAllAppealsByEmpId(Long empId, Integer type, Integer status, String start, String end){
        return appealMapper.getAppealsByEmp(empId,type,status,start,end);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void sendAppeal(Long appealId) {
        BgAppeal appeal = BgAppeal.builder()
                .id(appealId)
                .status(1)
                .build();
        appealMapper.updateById(appeal);
    }

    @Override
    public boolean isAppealAvailable(Long appealId) {
        BgAppeal bgAppeal = appealMapper.selectById(appealId);
        if (bgAppeal == null){
            throw new CustomException(ERROR_CODE,"该条记录不存在");
        }
        return bgAppeal.getStatus() == 0;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void deleteAppeal(Long appealId) {
        appealMapper.deleteById(appealId);
    }

    @Override
    public List<BgAppeal> getAllAppealsByCom(Long comId, Integer type, Integer status, String start, String end) {
        if (!companyService.isComInfoExisted(comId)) {
            // 说明该用户为普通管理员，则进行调取超级管理员的公司信息
            comId = adminRoleMapper.getAdminById(comId);
        }
        return appealMapper.getAppealsByCom(comId, type, status, start, end);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void approveAppeal(Long proId) {
        appealMapper.setIsAppealFalse(proId);
    }

    @Override
    public BgAppeal getAppealsById(Long appealId) {
        return appealMapper.selectById(appealId);
    }
}